package com.dzienniczekwedkarza.adapters;


import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ImageView;
import android.widget.TextView;

import com.dzienniczekwedkarza.R;
import com.dzienniczekwedkarza.models.AtlasRybModelDanych;

/**
 * @author Rafał Wysogląd
 * 
 */
public class ListViewStaticAtlasRybAdapter extends ArrayAdapter<AtlasRybModelDanych> implements Filterable {

	
	// Declare Variables - deklarowanie zmiennych
     private FischesFilter filter;
     private List<AtlasRybModelDanych> allModelItemsArray;
     private List<AtlasRybModelDanych> filteredModelItemsArray;
	 Context context;
     int layoutResourceId;  
	 AtlasRybModelDanych data[] = null;
	
	
	 
	
	 //konstruktor
	 public ListViewStaticAtlasRybAdapter(Context context, int layoutResourceId, AtlasRybModelDanych[] data) {
			
		 super(context, layoutResourceId, data);
		 this.layoutResourceId = layoutResourceId;
         this.context = context;
         this.data = data;
	 }
	 
  
  	 public View getView(int position, View convertView, ViewGroup parent)
	 {
		View row = convertView;
		AtlasRybModelDanychHolder holder = null; 
		 
		   if(row == null)
	        {
	            LayoutInflater inflater = ((Activity)context).getLayoutInflater();
	            row = inflater.inflate(layoutResourceId, parent, false);
	         
	            holder = new AtlasRybModelDanychHolder();
	            holder.imgIcon = (ImageView)row.findViewById(R.id.imgIcon);
	            holder.txtTitle = (TextView)row.findViewById(R.id.txtTitle);
	            holder.txtTitleLacina = (TextView)row.findViewById(R.id.txtTitleLacina);
	            holder.txtFamily = (TextView)row.findViewById(R.id.txtFamily);
	 
	            row.setTag(holder);
	        }
	        else
	        {
	            holder = (AtlasRybModelDanychHolder)row.getTag();
	        }
	 
	        AtlasRybModelDanych object = data[position];
	        
	        holder.imgIcon.setImageResource(object.getIcon());
	        holder.txtTitle.setText(object.getNazwaRyby());
	        holder.txtTitleLacina.setText(object.getNazwaRybyLacinska());
	        holder.txtFamily.setText(object.getNazwaRodziny());
	        

		return row;
		 
	 }
	 
	 
  	//statyczna klasa definiująca tzw. trzymacz, uchwyt do wiersza.
  	static class AtlasRybModelDanychHolder
  	{
  	        ImageView imgIcon;
  	        TextView  txtTitle;
  	        TextView  txtTitleLacina;
  	        TextView  txtFamily;
  	}
  	

  	
    @Override
    public Filter getFilter() {
        if (filter == null){
          filter  = new FischesFilter();
        }
        return filter;
    }
    
    
     
  	
   	private class FischesFilter extends Filter{

		@Override
		protected FilterResults performFiltering(CharSequence constraint) {
			
			 constraint = constraint.toString().toLowerCase();
			 FilterResults results = new FilterResults();
			
			 // We implement here the filter logic
			 if(constraint != null && constraint.toString().length() > 0)
             {
                 ArrayList<AtlasRybModelDanych> filteredItems = new ArrayList<AtlasRybModelDanych>();
                
                 for(int i = 0, l = allModelItemsArray.size(); i < l; i++)
                 {
                     AtlasRybModelDanych m = allModelItemsArray.get(i);
                     if(m.getNazwaRyby().toLowerCase().contains(constraint))
                         filteredItems.add(m);
                 }
                 results.count = filteredItems.size();
                 results.values = filteredItems;
             }
             else
             {
                 synchronized(this)
                 {
                     results.values = allModelItemsArray;
                     results.count = allModelItemsArray.size();
                 }
             }
             return results;
		
		}

		
		
		@SuppressWarnings("unchecked")
		@Override
		protected void publishResults(CharSequence constraint,	FilterResults results) {
		
			ArrayList<AtlasRybModelDanych> filteredModelItemsArray = (ArrayList<AtlasRybModelDanych>)results.values;
	            //notifyDataSetChanged();
	           /* clear();
	            for(int i = 0, l = filteredModelItemsArray.size(); i < l; i++)
	                add(filteredModelItemsArray.get(i));
	            */
			
	            if(results != null && results.count > 0) {
	                clear();
	                for (AtlasRybModelDanych i : filteredModelItemsArray) {
	                     add(i);
	            }
	                
	                
	            notifyDataSetChanged();
	        }
	   
		}
   	

  		
    }
}
